<html>
<head><meta charset="utf-8"><title>Generic associated constants · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20associated.20constants.html">Generic associated constants</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="238103676"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Generic%20associated%20constants/near/238103676" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Oliver E. Anderson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20associated.20constants.html#238103676">(May 10 2021 at 08:22)</a>:</h4>
<p>Does anyone know if there is work on allowing generic associated constants? <br>
I want to do something like this <br>
trait AbelianGroup {<br>
    const IDENTITY;<br>
} <br>
and not specify what type IDENTITY is until the trait is implemented.</p>



<a name="238104141"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Generic%20associated%20constants/near/238104141" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20associated.20constants.html#238104141">(May 10 2021 at 08:25)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">AbelianGroup</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">IDENTITY</span>: <span class="nc">T</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>This seems to compile on the Playground.</p>



<a name="238104461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Generic%20associated%20constants/near/238104461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Oliver E. Anderson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20associated.20constants.html#238104461">(May 10 2021 at 08:27)</a>:</h4>
<p><span class="user-mention silent" data-user-id="400735">Pointerbender</span> <a href="#narrow/stream/122651-general/topic/Generic.20associated.20constants/near/238104141">said</a>:</p>
<blockquote>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">AbelianGroup</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">IDENTITY</span>: <span class="nc">T</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>This seems to compile on the Playground.</p>
</blockquote>
<p>But isn't it then possible to implement it on the same type several times with different parameters T?</p>



<a name="238105075"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Generic%20associated%20constants/near/238105075" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20associated.20constants.html#238105075">(May 10 2021 at 08:32)</a>:</h4>
<p><span class="user-mention" data-user-id="390743">@Oliver E. Anderson</span>  indeed. If you want to limit it to one trait <code>impl</code> per type, then you can add an associated type like this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">AbelianGroup</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">Type</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">IDENTITY</span>: <span class="nc">Self</span>::<span class="n">Type</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="238164061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Generic%20associated%20constants/near/238164061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20associated.20constants.html#238164061">(May 10 2021 at 15:43)</a>:</h4>
<p>Does it work to just use <code>const IDENTITY: Self;</code>?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>